package com.sun.xacml.attr;

import java.net.URI;
import java.util.List;



import org.jdom.Namespace;
import org.jdom.input.DOMBuilder;
import org.w3c.dom.Element;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class PointAttribute extends AttributeValue {
	private static URI identifierURI;
	private static String srsName;//chua srsName
	private static Point geometry;//chua geometry cua POINT
	
	/**
	 * Constructor tao Point attribute tu jdomRoot(duoi dang gml encode) 
	 */
	public PointAttribute(org.jdom.Element jdomRoot){
		super(identifierURI);
		
		Namespace gml = null;
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);

		//lay srsname
		srsName = jdomRoot.getAttributeValue("srsName");
		
		//lay toa do cua Point
		org.jdom.Element coord = jdomRoot.getChild("coord", gml);
		String str = "";
		if (coord != null) {
			str = jdomRoot.getChild("coord", gml).getChild("X", gml).getValue(); //toa do x
			str += " " + jdomRoot.getChild("coord", gml).getChild("Y", gml).getValue(); //toa do y			
		} 
		else str = jdomRoot.getChild("coordinate", gml).getValue();//toa do x,y cua Point
		
		//tao gia tri jts
		try {
			geometry = (Point) new WKTReader().read("POINT(" + str + ")");				
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	/**
	 * Constructor tao Point attribute tu DomRoot 
	 */
	public PointAttribute(Element domRoot){
		super(identifierURI);
		
		//chuyen tu DOM sang jdom
		DOMBuilder domBuilder = new DOMBuilder();
		org.jdom.Element jdomRoot = domBuilder.build(domRoot);
		
		Namespace gml = null;
		List additionalNamespaces = jdomRoot.getAdditionalNamespaces();
		for (int ix = 0; ix < additionalNamespaces.size(); ix++)
			if ("gml".equals(((Namespace) additionalNamespaces.get(ix))
					.getPrefix()))
				gml = (Namespace) additionalNamespaces.get(ix);

		//lay srsName
		srsName = jdomRoot.getChild("Point", gml).getAttributeValue("srsName");
		
		//lay toa do cua Point
		String str = "";
		org.jdom.Element coord = jdomRoot.getChild("coord", gml);
		if (coord != null) {
			str = jdomRoot.getChild("Point", gml).getChild("coord", gml).getChild("X", gml).getValue();
			str += " " + jdomRoot.getChild("Point", gml).getChild("coord", gml).getChild("Y", gml).getValue();
		} else 	str = jdomRoot.getChild("Point", gml).getChild("coordinate",gml).getValue();
		
		//tao gia tri jts
		try {
			geometry = (Point) new WKTReader().read("POINT(" + str + ")");
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	//get geometry 
	public Point getGeometry() {
		return geometry;
	}	

	//get srsName
	public String getSrsName() {
		return srsName;
	}
	
	@Override
	public String encode() {
		// TODO Auto-generated method stub
		return geometry.toString();
	}
	
	public static AttributeValue getInstance(org.jdom.Element root) throws ParseException {
		// TODO Auto-generated method stub
		return getInstance((Element)root);
	}
	
	public static AttributeValue getInstance(Element root) throws ParseException {
		// TODO Auto-generated method stub
		return new PointAttribute(root);
	}
}
